//
//  CyclePlaceHolderTextField.m
//  MyCycleTool
//
//  Created by dihuijun on 17/7/6.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "CyclePlaceHolderTextField.h"
#import <objc/runtime.h>

static NSString *const textField_PlaceHolderLabel = @"_placeholderLabel.textColor";
static NSString *const textField_PlaceHolderLabelFont = @"_placeholderLabel.font";
static double tintHeight = 19;//光标高度
static NSString *const textKeyPath = @"text";//监听带入初始化值的数据
@implementation CyclePlaceHolderTextField

//纯代码
//- (instancetype)initWithCoder:(NSCoder *)coder
//{
//    self = [super initWithCoder:coder];
//    if (self) {
//        [self setup];
//    }
//    return self;
//}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
//xib
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
    
}
///初始化
- (void)setup{
    //1.设置光标颜色
    if (!self.c_tintColor) {
        self.c_tintColor = [UIColor lightGrayColor];
    }
    self.tintColor = self.c_tintColor;
    //2.设置默认的占位文字颜色
    if (!self.c_placeHolderNormalColor) {
        self.c_placeHolderNormalColor = [UIColor lightGrayColor];
    }
    if (!self.c_placeHolderNormalFont) {
        self.c_placeHolderNormalFont = self.font;
    }
    ///点击文本框后颜色
    if (!self.c_placeHolderHightLightedColor) {
        self.c_placeHolderHightLightedColor = [UIColor lightGrayColor];
    }
    if (!self.c_placeHolderHightLightedFont) {
//        self.c_placeHolderHightLightedFont = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        self.c_placeHolderHightLightedFont = self.font;
    }
    //设置默认颜色，字体
    if (@available(iOS 13.0,*)) {
        Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
        UILabel *placeholderLabel = object_getIvar(self, ivar);
        placeholderLabel.textColor = self.c_placeHolderNormalColor;
        placeholderLabel.font = self.c_placeHolderNormalFont;
        
    } else {
       [self setValue:self.c_placeHolderNormalColor forKeyPath:textField_PlaceHolderLabel];
        [self setValue:self.c_placeHolderNormalFont forKeyPath:textField_PlaceHolderLabelFont];
    }
    
    
    ///监听键盘出来的事件
    [self addTarget:self action:@selector(EditingDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
    ///监听键盘下去的事件
    [self addTarget:self action:@selector(EditingDidEnd:) forControlEvents:UIControlEventEditingDidEnd];
    ///监听text值的改变的事件
    [self addTarget:self action:@selector(textfieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    ///观察者监听初始值的改变
    [self addObserver:self forKeyPath:textKeyPath options:NSKeyValueObservingOptionNew context:nil];
}
///观察者监听初始值的改变
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:textKeyPath]) {
        if (self.text.length > 0) {
            [self textfieldDidChange:self];
        }
    }
}
- (void)dealloc {
    [self removeObserver:self forKeyPath:textKeyPath];
}
#pragma mark --- 1.监听键盘出来的事件
- (void)EditingDidBegin:(CyclePlaceHolderTextField *)textField{
    if(textField.text.length == 0){
        ///设置聚焦的占位文字颜色字体大小
        [self cycle_setHightedPlaceHolderColorAndFont];
    }
}
#pragma mark --- 2.监听键盘下去的事件
- (void)EditingDidEnd:(CyclePlaceHolderTextField *)textField{
    if (textField.text.length == 0) {
        ///设置聚焦的占位文字颜色字体大小
        [self cycle_setNormalPlaceHolderColorAndFont];
    }
}
#pragma mark --- 3.监听text值的改变的事件
- (void)textfieldDidChange:(CyclePlaceHolderTextField *)textField{
 
    if (textField.text.length > 0) {
       ///正在输入的文本框颜色和字体
        [self cycle_isEditingColorAndFont];
    }
    
    if (self.valueChangeBlock) {
        self.valueChangeBlock(textField, textField.text);
    }
}

/** 默认占位文字颜色 */
-(void)setC_placeHolderNormalColor:(UIColor *)c_placeHolderNormalColor{
    _c_placeHolderNormalColor = c_placeHolderNormalColor;
    //    [self setValue:self.c_placeHolderNormalColor forKeyPath:textField_PlaceHolderLabel];
}
- (void)setC_placeHolderNormalFont:(UIFont *)c_placeHolderNormalFont {
    _c_placeHolderNormalFont = c_placeHolderNormalFont;
    ///设置没聚焦的占位文字颜色字体大小
    [self cycle_setNormalPlaceHolderColorAndFont];
    
}

/** 点击文本框占位文字颜色 */
-(void)setC_placeHolderHightLightedColor:(UIColor *)c_placeHolderHightLightedColor{
    _c_placeHolderHightLightedColor = c_placeHolderHightLightedColor;
    //    [self setValue:self.c_placeHolderHightLightedColor forKeyPath:textField_PlaceHolderLabel];
}
- (void)setC_placeHolderHightLightedFont:(UIFont *)c_placeHolderHightLightedFont {
    _c_placeHolderHightLightedFont = c_placeHolderHightLightedFont;
    
}


/** 光标颜色颜色 */
-(void)setC_tintColor:(UIColor *)c_tintColor{
    _c_tintColor = c_tintColor;
    self.tintColor = c_tintColor;
}
/** 光标与文字的间距 */
-(void)setC_tintContentW:(CGFloat)c_tintContentW{
    _c_tintContentW = c_tintContentW;
}
/** 光标位置（目前居中，只需要左右设置） */
-(void)setC_tintX:(CGFloat)c_tintX{
    _c_tintX = c_tintX;
    
}


#pragma mark --- 4.改变光标位置以及光标跟内容之间的间距
// 重写来编辑区域，可以改变光标起始位置，以及光标最右到什么地方，placeHolder的位置也会改变
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
    if (self.c_tintContentW==0) {
        self.c_tintContentW = 8.00;
    }
    CGRect inset = CGRectMake(bounds.origin.x+self.c_tintContentW, bounds.origin.y, bounds.size.width -20, bounds.size.height);//更好理解些
    return inset;
}
///设置光标尺寸，位置
- (CGRect)caretRectForPosition:(UITextPosition *)position {
    CGRect originalRect = [super caretRectForPosition:position];
    originalRect.size.height = tintHeight;
    originalRect.size.width = 2;
    originalRect.origin.y = (self.bounds.size.height-originalRect.size.height)*0.5;
    return originalRect;
}

// 修改文本展示区域，一般跟editingRectForBounds一起重写
- (CGRect)textRectForBounds:(CGRect)bounds
{
    if (self.c_tintContentW==0) {
        self.c_tintContentW = 8.00;
    }
    CGRect inset = CGRectMake(bounds.origin.x+self.c_tintContentW, bounds.origin.y, bounds.size.width-25, bounds.size.height);//更好理解些
    return inset;
}

// 重写来编辑区域，可以改变光标起始位置，以及光标最右到什么地方，placeHolder的位置也会改变
-(CGRect)editingRectForBounds:(CGRect)bounds
{
    if (self.c_tintX==0) {
        self.c_tintX = 5.00;
    }
    CGRect inset;
    if (self.text.length > 0) {
        // 这里100可能需要自己调整一下使其居中即可
        inset = CGRectMake(bounds.origin.x + self.c_tintX, bounds.origin.y, bounds.size.width, bounds.size.height);//更好理解些
    }
    //    NSLog(@"%@",self.text);
    else {
        
        inset = CGRectMake(bounds.origin.x+self.c_tintX, bounds.origin.y, bounds.size.width, bounds.size.height);//更好理解些
    }
    return inset;
}

#pragma mark -- 设置没聚焦的占位文字颜色字体大小
///设置没聚焦的占位文字颜色字体大小
- (void)cycle_setNormalPlaceHolderColorAndFont {
    if (@available(iOS 13.0,*)) {
        Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
        UILabel *placeholderLabel = object_getIvar(self, ivar);
        placeholderLabel.textColor = self.c_placeHolderNormalColor;
        placeholderLabel.font = self.c_placeHolderNormalFont;
    } else {
        [self setValue:self.c_placeHolderNormalColor forKeyPath:textField_PlaceHolderLabel];
        [self setValue:self.c_placeHolderNormalFont forKeyPath:textField_PlaceHolderLabelFont];

    }
}
#pragma mark -- 设置聚焦的占位文字颜色字体大小
///设置聚焦的占位文字颜色字体大小
- (void)cycle_setHightedPlaceHolderColorAndFont {
    if (@available(iOS 13.0,*)) {
        Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
        UILabel *placeholderLabel = object_getIvar(self, ivar);
        placeholderLabel.textColor = self.c_placeHolderHightLightedColor;
        placeholderLabel.font = self.c_placeHolderHightLightedFont;
    } else {
        [self setValue:self.c_placeHolderHightLightedColor forKeyPath:textField_PlaceHolderLabel];
        [self setValue:self.c_placeHolderHightLightedFont forKeyPath:textField_PlaceHolderLabelFont];

    }
}
#pragma mark --- 正在输入的文本框颜色和字体
///正在输入的文本框颜色和字体
- (void)cycle_isEditingColorAndFont {
    if (self.c_isEditingColor) {
        self.font = self.c_isEditingFont;
    }
    if (self.c_isEditingFont) {
        self.textColor = self.c_isEditingColor;
    }
}
@end
